import { BeanType, ReflectUtil } from "../di"
import { buildMethodKey } from "../common"

// export function Bean(id: string = null) {
//   return function (target) {
//     ReflectUtil.defineBean(target, BeanType.Component, id)
//     return target
//   }
// }

function wrapBeanConstructor(fn) {
  // 这个this 指的是 fn 所在的 类 实例化后的对象
  function wrapped(this, ...args: unknown[]) {
    const runOriginal = () => {
      return fn.apply(this, [...args])
    }
    return runOriginal()
  }

  return wrapped
}

export function Bean(id: string = null) {
  return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
    const paramTypes = ReflectUtil.getParamTypes(target, propertyKey)
    const returnType = ReflectUtil.getReturnType(target, propertyKey)
    descriptor.value = wrapBeanConstructor(descriptor.value)
    ReflectUtil.defineBean(id || propertyKey, returnType, paramTypes, descriptor.value)
  }
}
